.TH std::execution::seq,std::execution::par,std::execution::par_unseq,std::execution::unseq 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::execution::seq,std::execution::par,std::execution::par_unseq,std::execution::unseq \- std::execution::seq,std::execution::par,std::execution::par_unseq,std::execution::unseq

.SH Synopsis
   Defined in header <execution>
   inline constexpr                                                       \fI(since C++17)\fP
   std::execution::sequenced_policy seq { /* unspecified */ };
   inline constexpr                                                       \fI(since C++17)\fP
   std::execution::parallel_policy par { /* unspecified */ };
   inline constexpr
   std::execution::parallel_unsequenced_policy par_unseq { /*             \fI(since C++17)\fP
   unspecified */ };
   inline constexpr                                                       \fI(since C++20)\fP
   std::execution::unsequenced_policy unseq { /* unspecified */ };

   The execution policy types

     * std::execution::sequenced_policy,
     * std::execution::parallel_policy,
     * std::execution::parallel_unsequenced_policy, and
     * std::execution::unsequenced_policy

   have the following respective instances:

     * std::execution::seq,
     * std::execution::par,
     * std::execution::par_unseq, and
     * std::execution::unseq.

   These instances are used to specify the execution policy of parallel algorithms,
   i.e., the kinds of parallelism allowed.

   Additional execution policies may be provided by a standard library implementation
   (possible future additions may include std::parallel::cuda and
   std::parallel::opencl).

.SH Example


// Run this code

 #include <algorithm>
 #include <chrono>
 #include <cstdint>
 #include <iostream>
 #include <random>
 #include <vector>

 #ifdef PARALLEL
 #include <execution>
     namespace execution = std::execution;
 #else
     enum class execution { seq, unseq, par_unseq, par };
 #endif

 void measure([[maybe_unused]] auto policy, std::vector<std::uint64_t> v)
 {
     const auto start = std::chrono::steady_clock::now();
 #ifdef PARALLEL
     std::sort(policy, v.begin(), v.end());
 #else
     std::sort(v.begin(), v.end());
 #endif
     const auto finish = std::chrono::steady_clock::now();
     std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(finish - start)
               << '\\n';
 };

 int main()
 {
     std::vector<std::uint64_t> v(1'000'000);
     std::mt19937 gen {std::random_device{}()};
     std::ranges::generate(v, gen);

     measure(execution::seq, v);
     measure(execution::unseq, v);
     measure(execution::par_unseq, v);
     measure(execution::par, v);
 }

.SH Possible output:

 // online GNU/gcc compiler (PARALLEL macro is not defined)
 81ms
 80ms
 79ms
 78ms

 // with g++ -std=c++23 -O3 ./test.cpp -ltbb -DPARALLEL
 165ms
 163ms
 30ms
 27ms

.SH See also

   sequenced_policy
   parallel_policy
   parallel_unsequenced_policy
   unsequenced_policy          execution policy types
   \fI(C++17)\fP                     \fI(class)\fP
   \fI(C++17)\fP
   \fI(C++17)\fP
   (C++20)
